package m202406.d10;

import java.util.*;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/10 12:22
 */
public class Q47 {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        for (int num : nums) {
            list.add(num);
        }
        doPermute(0, list, res);
        return res;
    }

    private void doPermute(int start, List<Integer> nums, List<List<Integer>> res) {
        if (start == nums.size()) {
            res.add(new ArrayList<>(nums));
            return;
        }
        Set<Integer> set = new HashSet<>();
        for (int i = start; i < nums.size(); i++) {
            if (set.contains(nums.get(i))) {
                continue;
            }
            set.add(nums.get(i));
            Collections.swap(nums, start, i);
            doPermute(start + 1, nums, res);
            Collections.swap(nums, start, i);
        }
    }
}
